/**
 * Created by xuan on 15/12/11.
 *  artDialog api : http://aui.github.io/artDialog/doc/index.html
 */
define(function (require) {
    'use strict';
    require('angular-mocks');
    require('static_public/js/angular_directive/modal.js');
    var dialog = require('dialog');
    var util = require('util');
    describe('modal', function () {
        var scope,controllerProvider, $compile, $document,$templateCache;
        beforeEach(module('angular.directive.modal'));
        beforeEach(module(function($controllerProvider) {
            controllerProvider = $controllerProvider;
        }));
        beforeEach(inject(function ($rootScope, _$compile_, _$document_,_$templateCache_) {
            scope = $rootScope;
            $compile = _$compile_;
            $document = _$document_;
            $templateCache = _$templateCache_;
        }));
        afterEach(function(){
            dialog.getCurrent() && dialog.getCurrent().remove();
        });
        it('标签属性custom-modal=""',function(){
            spyOn(util,'alert');
            var element = $compile('<div custom-modal></div>')(scope);
            element.click();
            expect(util.alert).toHaveBeenCalledWith('custom-modal属性不能为空！');
        });
        it('不传url或者templateStr',function(){
            spyOn(util,'alert');
            controllerProvider.register('customController',function(){});
            var element = $compile('<div custom-modal="customController"></div>')(scope);
            element.click();
            expect(util.alert).toHaveBeenCalledWith('controller需要配置url或者templateStr参数');
        });
        it('控制器带templateStr属性',function(){
            var tempScope;
            controllerProvider.register('customController',function($scope){
                $scope.templateStr = '我是弹出层字符串';
                tempScope = $scope;
            });
            var element = $compile('<div custom-modal="customController"></div>')(scope);
            element.click();
            expect(tempScope.dialog).toBeDefined();
        });
        it('控制器带url属性',function(){
            $templateCache.put('modal-content.html','我是远程加载内容！');
            var tempScope;
            controllerProvider.register('customController',function($scope){
                $scope.url = 'modal-content.html';
                tempScope = $scope;
            });
            var element = $compile('<div custom-modal="customController"></div>')(scope);
            element.click();
            scope.$digest();
            expect(tempScope.dialog).toBeDefined();
        });
        it('控制器带url和templateStr属性',function(){
            var tempScope;
            controllerProvider.register('customController',function($scope){
                $scope.templateStr = '我是弹出层字符串';
                $scope.url = 'modal-content.html';
                tempScope = $scope;
            });
            var element = $compile('<div custom-modal="customController"></div>')(scope);
            element.click();
            expect(tempScope.dialog).toBeDefined();
        });
        it('标签包含custom-modal-data',function(){
            var tempScope;
            controllerProvider.register('customController',function($scope){
                $scope.templateStr = '我是弹出层字符串';
                tempScope = $scope;
            });
            var element = $compile('<div custom-modal="customController" custom-modal-data="{id:1}"></div>')(scope);
            element.click();
            expect(tempScope.data.id).toBe(1);
        });
        it('controller的scope包含init方法',function(){
            var tempScope,callInit = false;
            controllerProvider.register('customController',function($scope){
                $scope.templateStr = '<p>我是弹出层字符串</p>';
                $scope.init = function () {
                    callInit = true;
                };
                tempScope = $scope;
            });
            var element = $compile('<div custom-modal="customController"></div>')(scope);
            element.click();
            scope.$digest();
            expect(callInit).toBe(true);
            tempScope.dialog.remove();
            expect(tempScope.dialog.open).toBe(false);
        });
        it('controller的scope包含title、cancelValue、okValue属性',function(){


        });
        it('controller的scope包含ok方法',function(){
            var tempScope,okCall = false;
            controllerProvider.register('customController',function($scope){
                $scope.templateStr = '<p>我是弹出层字符串</p>';
                $scope.ok = function () {
                    okCall = true;
                };
                tempScope = $scope;
            });
            var element = $compile('<div custom-modal="customController"></div>')(scope);
            element.click();
            scope.$digest();
            tempScope.dialog._popup.find('[i-id=ok]').trigger('click');
            expect(okCall).toBe(true);
        });
        it('controller的scope包含cancel方法',function(){

        });
        it('dialog关闭时销毁scope',function(){
            var tempScope,callDestroy = false;
            controllerProvider.register('customController',function($scope){
                $scope.templateStr = '我是弹出层字符串';
                $scope.ok = function () {
                };
                $scope.$on('$destroy',function(){
                    callDestroy = true;
                });
                tempScope = $scope;
            });
            var element = $compile('<div custom-modal="customController"></div>')(scope);
            element.click();
            scope.$digest();
            tempScope.dialog._popup.find('[i-id=ok]').trigger('click');
            expect(tempScope.dialog.open).toBe(false);
            expect(callDestroy).toBe(true);
        });
        it('dialog自动show',function(){
            var tempScope;
            controllerProvider.register('customController',function($scope){
                $scope.templateStr = '我是弹出层字符串';
                tempScope = $scope;
            });
            var element = $compile('<div custom-modal="customController"></div>')(scope);
            element.click();
            expect(tempScope.dialog.open).toBe(true);
        });

        it('dialog延迟显示',function(){
            var tempScope;
            controllerProvider.register('customController',function($scope){
                $scope.templateStr = '我是弹出层字符串';
                $scope.delayShow = true;
                tempScope = $scope;
            });
            var element = $compile('<div custom-modal="customController"></div>')(scope);
            element.click();
            expect(tempScope.dialog.open).toBe(false);
        });
        it('dialog延迟关闭',function(){
            var tempScope;
            controllerProvider.register('customController',function($scope){
                $scope.templateStr = '我是弹出层字符串';
                $scope.ok = function () {
                    return false;
                };
                tempScope = $scope;
            });
            var element = $compile('<div custom-modal="customController"></div>')(scope);
            element.click();
            tempScope.dialog._popup.find('[i-id=ok]').trigger('click');
            expect(tempScope.dialog.open).toBe(true);
        });
        it('dialog form表单校验',function(){
            var tempScope;
            controllerProvider.register('customController',function($scope){
                $scope.templateStr = '<form name="tempForm"><input type="text" ng-model="name" required/></form>';
                $scope.ok = function () {
                    return true;
                };
                tempScope = $scope;
            });
            var element = $compile('<div custom-modal="customController"></div>')(scope);
            element.click();
            tempScope.dialog._popup.find('[i-id=ok]').trigger('click');
            expect(tempScope.dialog.open).toBe(true);
        });
        it('dialog form表单校验',function(){

        });
    });
});